/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrpe.bsi.dao;

import br.ufrpe.bsi.bean.Compromisso;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Daniel
 */
public class CompromissoDAO extends Conexao{
    public CompromissoDAO(){
        super();
    }
    
    public void inserir(Compromisso compromisso) throws SQLException, ClassNotFoundException{
        String sql= "INSERT INTO compromisso(descricao,dataInicio,dataFim,usuario_login) VALUES (?,?,?,?)";
        PreparedStatement ps = (PreparedStatement) getPreparedStatement(sql);
        ps.setString(1, compromisso.getDescricao());
        ps.setTimestamp(2,new Timestamp(compromisso.getDataInicio().getTime()));
        ps.setTimestamp(3,new Timestamp(compromisso.getDataFim().getTime()));
        ps.setString(4,compromisso.getUsuario().getLogin());
        ps.execute();
        ps.close();
        
    }
    
    public void editar(Compromisso compromisso) throws ClassNotFoundException, SQLException{
        String sql= "UPDATE compromisso SET descricao=?,dataInicio=?,dataFim=? WHERE idCompromisso=?";
        PreparedStatement ps = (PreparedStatement) getPreparedStatement(sql);
        ps.setString(1, compromisso.getDescricao());
        ps.setTimestamp(2,new Timestamp(compromisso.getDataInicio().getTime()));
        ps.setTimestamp(3,new Timestamp(compromisso.getDataFim().getTime()));      
        ps.setInt(4, compromisso.getIdCompromisso());
        ps.execute();
        ps.close();
        
    }  
    
    public void excluir (Compromisso compromisso) throws ClassNotFoundException, SQLException{
        String sql = "DELETE FROM compromisso WHERE idCompromisso=?";
        PreparedStatement ps = (PreparedStatement) getPreparedStatement(sql);
        ps.setInt(1, compromisso.getIdCompromisso());
        ps.execute();
        ps.close();
    }
    
    public List<Compromisso> retornarCompromissos(String login) throws ClassNotFoundException, SQLException{
        List<Compromisso> lista = new LinkedList<Compromisso>(); 
        String sql = "SElECT * FROM compromisso WHERE usuario_login='"+login+"' ORDER BY dataInicio";
        Statement st = (Statement) getCon().createStatement();
        ResultSet rs = st.executeQuery(sql);
        UsuarioDAO usuarioDAO = new UsuarioDAO();
        while(rs.next()){
            Compromisso comp = new Compromisso();
            comp.setUsuario(usuarioDAO.retornar(login));
            comp.setIdCompromisso(rs.getInt("idCompromisso"));
            comp.setDescricao(rs.getString("descricao"));
            comp.setDataInicio(rs.getTimestamp("dataInicio"));
            comp.setDataFim(rs.getTimestamp("dataFim"));
            lista.add(comp);
        }
        return lista;
    }
}
